﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;

public partial class wxAdmin : System.Web.UI.Page
{
    StringBuilder sb = new StringBuilder();
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            //if (Session["username"] == null)
            //{
            //    Response.Redirect("~/Account/Login.aspx");
            //}
            //InitTree();
            //xxx.Text = Init();
            //Response.Write(Init());
        }
        
    }
    #region 递归绑定同一个表数据
    private void InitTree()
    {
        DataTable dt = GetTreeViewTable();
        DataView dv = new DataView(dt);
        dv.RowFilter = "r1=0"; //添加过滤条件.过滤出一级菜单
        tv1.ShowCheckBoxes = TreeNodeTypes.All;
        foreach (DataRowView drv in dv)
        {
            TreeNode node = new TreeNode();
            node.Text = drv["text_Content"].ToString();
            node.Value = drv["id_auto"].ToString();
            node.Expanded = false;
            tv1.Nodes.Add(node);
            AddReplies(dt, node);
        }
    }

    private string InitXXX()
    {
        //string str = "";
       // string ret = "";
        DataTable dt = GetTreeViewTable();
        DataView dv = new DataView(dt);
        dv.RowFilter = "r1=0"; //添加过滤条件.过滤出一级菜单
        foreach (DataRowView drv in dv)
        {
            TreeNode node = new TreeNode();
            node.Text = drv["text_Content"].ToString();
            node.Value = drv["id_auto"].ToString();
            string str = "<p><a herf='#'>" + drv["text_Content"].ToString() + "</a></p>";
            sb.Append(str);
            AddReplies1(dt, node);
        }

        return sb.ToString();
    }
    private void AddReplies1(DataTable dt, TreeNode node)
    {
        DataView dv = new DataView(dt);
        dv.RowFilter = "r1='" + node.Value + "'";
        foreach (DataRowView row in dv)
        {
            //sb.Append("<ul>");
            TreeNode replyNode = new TreeNode();
            replyNode.Text = row["news_Title"].ToString();
           // replyNode.NavigateUrl = row["news_Url"].ToString();
            replyNode.Value = row["id_auto"].ToString();
            string str = "<ul><li><a href='"+row["news_Url"]+"'>" + replyNode.Text+"</a>";
            sb.Append(str);
            AddReplies1(dt, replyNode);
            sb.Append("</ul>");
        }
    }


    private DataTable GetTreeViewTable()
    {
        //string constring = System.Configuration.ConfigurationSettings.AppSettings["connection"];
        string constring = "";
        SqlConnection con = new SqlConnection(constring);
        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM MpRespMsg", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }

    private void AddReplies(DataTable dt, TreeNode node)
    {
        DataView dv = new DataView(dt);
        dv.RowFilter = "r1='" + node.Value + "'";
        foreach (DataRowView row in dv)
        {
            TreeNode replyNode = new TreeNode();
            replyNode.Text = row["news_Title"].ToString();
            replyNode.NavigateUrl = row["news_Url"].ToString();
            replyNode.Value = row["id_auto"].ToString();
            replyNode.Expanded = false;
            node.ChildNodes.Add(replyNode);
            AddReplies(dt, replyNode);
        }
    }
    #endregion
}